<?php
namespace Api\Controller;
use Think\Controller;
class ManageController extends CommonController
{

    //初始化
    function _initialize() {
        parent::_initialize();
        if(!$this->uinfo){
            $this->ajaxErrReturn('请先登录');
        }
    }

    //各角色数据
    function roleList(){
        $model = M('Admin');
        $datas = $model->field('count(aId) as count,atype')->group('atype')->select();
        $array = array(
            0=>'管理员',
            1=>'业务员',
            2=>'财务',
            3=>'采购',
            4=>'仓库管理',
            5=>'厂长',
            6=>'小组管理',
            7=>'工厂',
            10=>'未知',
        );
        $list = [];
        foreach($datas as $key=>$data){
            if($data['atype']==7){
                unset($datas[$key]);
            }else{
                $atype = $data['atype'];
                $data['title'] = $array[$atype];
                $list[] = $data;
            }
        }
        $wdata['atype'] = 7;
        $wdata['stype'] = 0;
        $bc_num = $model->where($wdata)->count();
        $data['title'] = '本厂';
        $data['count'] = $bc_num;
        $list[] = $data;
        $wdata['stype'] = 1;
        $ww_num = $model->where($wdata)->count();
        $data['title'] = '委外';
        $data['count'] = $ww_num;
        $list[] = $data;
        $this->ajaxSucReturn($list);
    }

    public function add()
    {
        $atype = $_GET['atype'];
        if ($atype == 1) {
            $cc = '业务员';
        } else {
            $cc = '管理员';
        }
        $this->assign("atype", $atype);
        $this->assign("cctt", $cc);
        $this->display();
    }

    //添加权限
    public function addc()
    {
        //权限列表
        $menus = M("menus");
        $id = $_GET['id'];
        $menus_list = $menus->where(['pid' => 0])->select();
        if ($menus_list) {
            foreach ($menus_list as $k => $val) {
                $menus_list[$k]['child'] = $menus->where(['pid' => $val['id']])->select();
            }
        }
        $this->assign("id", $id);
        $this->assign("menus_list", $menus_list);
        $this->display();
    }

    public function addqx()
    {

        $post = $_POST;
        $id = $post['id'];
        $roles = M("roles");
        $dat = [];
        $aa = $post['aa'];
        $bb = $post['bb'];
        $m1 = serialize($aa);
        $m2 = serialize($bb);
        $action_list = $m1 . '#_#' . $m2;
        $dat['action_list'] = $action_list;
        $result = $roles->where(['id' => $id])->save($dat);
        if ($result) {
            $this->success("编辑成功", U("roles_list"));
        } else {
            $this->error("编辑失败");
        }
    }

    //添加用户
    public function addAction(){
        $admin = M("admin");
        $role = M("roles");
        $aUser = $data["aUser"] = $_POST["aUser"];
        $aUserN = $admin->where("aUser='{$aUser}'")->count();
        if ($aUserN > 0) {
            $this->ajaxErrReturn("用户名已存在，请更换");
        }
        $post = I('post.');
        if(!$post['aUser']){
            $this->ajaxErrReturn("用户名必须");
        }
        if(!$post['aPwd']){
            $this->ajaxErrReturn("密码必须");
        }
        if(!$post['aPwd']){
            $this->ajaxErrReturn("密码必须");
        }
        $length = mb_strlen($aUser,"utf-8");
        if($length>16){
            $this->ajaxErrReturn('用户账号长度超出');
        }
        $data["avatar"] ='https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/2023/03/29/6423aafa356d3.jpg';
        $data["aName"] = $post["aName"] ? : '';
        $data["aPwd"] = md5($post["aPwd"]);
        $data["aSex"] = $post["aSex"];
        $atype=$role->where(['id'=>$post["atype"]])->getField('atype'); 
        $data["atype"] = $atype;
        if($post["atype"]==13){
            $data["stype"] = 1;//委外
        }
        if($post["atype"]==14){
            $data["stype"] = 2;//外协
        }
        $data["add_time"] = date('Y-m-d H:i:s',time());
        $aTel = $data["aTel"] = $post["aTel"];
        $aTelN = $admin->where("aTel='{$aTel}'")->count();
        if ($aTelN > 0) {
            $this->ajaxErrReturn("手机号码已存在，请更换");
        }
        $result = $admin->add($data);
        if ($result) {
            $this->ajaxSucReturn("添加成功");
        } else {
            $this->ajaxErrReturn("添加失败");
        }
    }

    public function lists()
    {
        $atype = $_GET['atype'];
        if ($atype == 1) {
            $cc = '业务员';
            $url = "../salesman/index";
        } elseif ($atype == 2) {
            $cc = '财务管理';
            $url = "../Finance/index";
        } elseif ($atype == 3) {
            $cc = '采购管理';
            $url = "../Purchasing/index";
        } elseif ($atype == 4) {
            $cc = '仓库管理';
            $url = "../Warehouse/index";
        } elseif ($atype == 5) {
            $cc = '厂长/主任';
            $url = "../Workshop/index";
        } elseif ($atype == 7) {
            $cc = '委外工厂';
            $url = "../Cordination/index";
        } else {
            $cc = '管理员';
        }
        $admin = M("admin");
        $where = [];
        if ($atype) {
            $where['atype'] = $atype;
        } else {
            $where['atype'] = 0;
        }
        $rs_admin = $admin->where($where)->order("aId asc")->select();
        $loginAid = session("aId");
        $this->assign("loginAid", $loginAid);
        $this->assign("atype", $atype);
        $this->assign("cctt", $cc);
        $this->assign("url", $url);
        $this->assign("rs_admin", $rs_admin);
        $aPowers = session("aPowers");
        $power = explode(",", $aPowers);
        $this->assign("power", $power);
        $this->assign("h5link", C("h5link"));

        $this->display();
    }

    //权限列表
    public function roles_list()
    {

        $roles = M("roles");
        $roles_list = $roles->order("id asc")->select();
        $this->assign("roles_list", $roles_list);
        $this->display();
    }

    public function update()
    {

        $aId = $_GET["aId"];
        $admin = D("Admin");
        $rs_admin = $admin->where("aId=$aId")->find();
        $this->assign("rs_admin", $rs_admin);
        $powers = explode(",", $rs_admin["aPowers"]);
        $this->assign("powers", $powers);
        $loginAid = session("aId");
        $this->assign("loginAid", $loginAid);
        $this->display();
    }

    //账号更新
    public function updateAction()
    {

        $post = I('post.');
        $aId = $post["aId"];
        if(empty($aId)){
            $this->ajaxErrReturn("用户ID不能为空");
        }
        $admin = M("admin");
        $admin_info = $admin->where("aId=$aId")->find();
        if(empty($admin_info)){
            $this->ajaxErrReturn("用户数据不存在");
        }
        
        $data["aName"] = $post["aName"];
        $aOldPwd = $post["aOldPwd"];
        if ($aOldPwd != null) {
            $aOldPwdMd5 = md5($aOldPwd);
            $rs_pwd = $admin->where("aId=$aId")->field("aPwd")->find();
            if ($aOldPwdMd5 != $rs_pwd["aPwd"]) {
                $this->ajaxErrReturn("原密码错误");
            }
            $aNewPwd = $_POST["aNewPwd"];
            if ($aNewPwd == null) {
                $this->ajaxErrReturn("新密码不能为空");
            }
            $data["aPwd"] = md5($aNewPwd);
        }

        $data["aSex"] = $post["aSex"];
        $aTel = $data["aTel"] = $post["aTel"];
        $rs_aTelN = $admin->where("aTel='{$aTel}' AND aId!=$aId")->count();
        if ($rs_aTelN > 0) {
            $this->ajaxErrReturn("该电话号码已经存在");
        }
        $aEmail = $data["aEmail"] = $post["aEmail"] ? : '';
        if ($aEmail != null) {
            $rs_aEmailN = $admin->where("aEmail='{$aEmail}' AND aId!=$aId")->count();
            if ($rs_aEmailN > 0) {
                $this->ajaxErrReturn("该邮箱账户已经存在");
            }
        }
        $powers1 = $post["powers1"];
        if ($powers1 == null) {
            $powers1 = 0;
        }
        $powers2 = $post["powers2"];
        if ($powers2 == null) {
            $powers2 = 0;
        }
        $powers3 = $post["powers3"];
        if ($powers3 == null) {
            $powers3 = 0;
        }
        $powers4 = $post["powers4"];
        if ($powers4 == null) {
            $powers4 = 0;
        }
        $powers5 = $post["powers5"];
        if ($powers5 == null) {
            $powers5 = 0;
        }
        $powers6 = $post["powers6"];
        if ($powers6 == null) {
            $powers6 = 0;
        }
        $powers7 = $post["powers7"];
        if ($powers7 == null) {
            $powers7 = 0;
        }
        $powers8 = $post["powers8"];
        if ($powers8 == null) {
            $powers8 = 0;
        }
        $powers9 = $post["powers9"];
        if ($powers9 == null) {
            $powers9 = 0;
        }
        if ($aId != 1) {
            $data["aPowers"] = $powers1 . "," . $powers2 . "," . $powers3 . "," . $powers4 . "," . $powers5 . "," . $powers6 . "," . $powers7 . "," . $powers8 . "," . $powers9;
        }
        $data["atype"] = $post["atype"];
        $data["dept_name"] = $post["dept_name"];
        $data["sub_dept_name"] = $post["sub_dept_name"];
        $data["avatar"] = $post["avatar"];
        //dump($data);exit;
        $result = $admin->where("aId=$aId")->save($data);
        if ($result) {
            $this->ajaxSucReturn("修改成功");
        } else {
            $this->ajaxErrReturn("修改失败");
        }
    }

    public function used()
    {

        $aId = $_GET["aId"];
        $ast = $_GET["ast"];
        if ($ast == 0) {
            $data["aStatic"] = 0;
        } else {
            $data["aStatic"] = 1;
        }
        $admin = M("admin");
        $result = $admin->where("aId=$aId")->save($data);
        if ($result) {
            $this->success("设置成功");
        } else {
            $this->error("设置失败");
        }
    }

    public function del()
    {

        $aId = $_GET["aId"];
        $admin = M("admin");
        $result = $admin->where("aId=$aId")->delete();
        if ($result) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }
}
